<template>
  <div class="app-container">

    <!-- 查询和其他操作 -->
    <div class="filter-container search_pc_az">
      <el-date-picker
        v-model="dateVal"
        type="daterange"
        align="right"
        unlink-panels
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        class="el_date_picker"
        @change="selectDate"/>
      <el-button type="primary" icon="el-icon-search" class="hidden-xs-only" @click="handleFilter">查 找</el-button>
      <el-button type="primary" icon="el-icon-search" class="hidden-sm-and-up el_button2" @click="handleFilter">查 找</el-button>
    </div>
    <!-- <div class="filter-container">
      <el-date-picker
        v-model="dateVal"
        :picker-options="pickerOptions"
        type="daterange"
        align="right"
        unlink-panels
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        style="width:400px;"
        @change="selectDate"/>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查找</el-button>
    </div> -->

    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" element-loading-text="正在查询中。。。" border fit highlight-current-row>

      <el-table-column align="center" label="姓名" prop="username" min-width="90"/>
      <el-table-column align="center" label="编号" prop="userId" min-width="180"/>
      <el-table-column align="center" label="最近到访记录" prop="detail" min-width="200"/>
      <el-table-column :formatter="formatDate" align="center" label="最近到访时间" prop="createTime" min-width="150" />
      <el-table-column label="操作" align="center" min-width="90">
        <template slot-scope="{row}">
          <el-button type="primary" @click="more(row.userId)">更多</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

  </div>
</template>

<script>
import { fetchRecordList } from '@/api/remark'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import { getShopId } from '@/utils/auth'

export default {
  name: 'User',
  components: { Pagination },
  data() {
    return {
      dateVal: '',
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        shopId: getShopId(),
        page: 1,
        limit: 20
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    selectDate(data) {
      if (data === null || data.length < 1) {
        this.listQuery.start = ''
        this.listQuery.end = ''
      } else {
        this.listQuery.start = new Date(data[0].getTime() + 8 * 60 * 60 * 1000)
        this.listQuery.end = new Date(data[1].getTime() + 32 * 60 * 60 * 1000 - 1)
      }
    },
    more(userId) {
      this.$router.push({ path: '/visit/more', query: { userId: userId }})
    },
    getList() {
      this.listLoading = true
      fetchRecordList(this.listQuery).then(response => {
        this.list = response.data.data.list
        this.total = response.data.data.total
        this.listLoading = false
      }).catch(() => {
        this.list = []
        this.total = 0
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    formatDate(row, column, cellValue) {
      var t = new Date(row.updateTime)
      return t.getFullYear() + '-' + (t.getMonth() + 1) + '-' + t.getDate() + ' ' + t.getHours() + ':' +
       t.getMinutes() + ':' + t.getSeconds()
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.app-container{
  width: 100%;
  .search_pc_az{
    margin: 0 auto;
    min-width: 225px;
    max-width: 590px;
    width: 100%;
    .el_date_picker{
      max-width: 490px;
      width: 100%;
      margin-bottom: 10px;
    }
    .el_button2{
      max-width: 490px;
      width: 100%;
      margin: 0;
    }
  }
}
</style>
